﻿using SL.IdentityModel.Services;

namespace IssueVision.EntityModel
{
    /// <summary>
    /// Issue class client-side extensions
    /// </summary>
    public partial class Issue
    {
        /// <summary>
        /// Business logic:
        /// Admin user can read/update any issue, and
        /// normal user can only read/update issues assigned to them
        /// or issues created by them and have not assigned to anyone.
        /// </summary>
        /// <returns></returns>
        public bool IsIssueReadOnly()
        {
            string userName = ClaimsIdentitySessionManager.Current.User.Identity.Name;

            // Admin user can read/update any issue
            if (ClaimsIdentitySessionManager.Current.User.IsInRole(IssueVisionServiceConstant.UserTypeAdmin))
                return false;

            // normal user can only read/update issues assigned to them
            // or issues created by them and have not assigned to anyone.
            if (AssignedToID != null && AssignedToID == userName)
                return false;
            if (AssignedToID == null && OpenedByID == userName)
                return false;

            return true;
        }
    }
}
